package com.subway.babynote.syncservice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.pWrapper;
import com.subway.babynote.syncdata.wrapper;


import android.content.Context;
//import android.net.http.AndroidHttpClient;
import android.os.Handler;
import android.util.Log;

public class httpUtil {
	protected static final String client_agent="baobaoyinshijilu-v1";
	protected static final String base_url = "http://bbjilu.com:8484/foodserver/";
	protected static final int REGISTRATION_TIMEOUT = 120000;
	private HttpClient hc = null;
	
	public httpUtil()
	{
		
	}
	public boolean init()
	{		
		if(hc == null)
			hc = new DefaultHttpClient();
		final HttpParams params = hc.getParams();
        HttpConnectionParams.setConnectionTimeout(params,
            REGISTRATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
        ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
		return true;		
	}
	public boolean init_mainthread()
	{		
		if(hc == null)
			hc = new DefaultHttpClient();
		final HttpParams params = hc.getParams();
        HttpConnectionParams.setConnectionTimeout(params,
            REGISTRATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
        ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
		return true;		
	}	 
	public boolean send(String servlet, List<NameValuePair> params, wrapper<JSONObject> pjson_resp) 
	{
		HttpEntity entity = null;
		HttpResponse resp = null;
		JSONObject json_resp = null;
        try 
        {
            entity = new UrlEncodedFormEntity(params);
        } 
        catch (final UnsupportedEncodingException e) 
        {
            // this should never happen.
        	Log.i("UrlEncodedFormEntity", "error");
            return false;
        }
        final HttpPost post = new HttpPost(base_url+servlet);
        post.addHeader(entity.getContentType());
        post.setEntity(entity);
        try 
        {
            resp = hc.execute(post);
            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
            {                
	            final String response = EntityUtils.toString(resp.getEntity());
	            json_resp = new JSONObject(response);
	            pjson_resp.set(json_resp);
	            return true;
            } else 
            {                   
                return false;
            }
        } 
        catch (final IOException e) 
        {            
            return false;
        }
        catch (final JSONException je) 
        {            
            return false;
        } 
	}
}
